package com.logicwin.aibala.services.file;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.logicwin.aibala.action.LoadStartUpAction;
import com.logicwin.aibala.com.GlobalConfig;
import com.logicwin.aibala.services.net.NetConnection;
import com.logicwin.aibala.ui.screens.screenmain.ScreenHome;
import com.logicwin.aibala.utils.MyLog;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;

public class AsyncImage implements Runnable {

	private final static Handler handler = new Handler();

	private static Vector<Object[]> loadImageVector = new Vector<Object[]>();

	private static Thread myThread = null;

	private static boolean threadRuning = true;

	private static void startThread() {
		if (myThread == null) {
			myThread = new Thread(new AsyncImage());
			myThread.start();
		} else {
			if (!myThread.isAlive())
				myThread.notify();
		}
	}

	/**
	 * 
	 * @param imageUrl
	 *            图像url地址
	 * @param callback
	 *            回调接口
	 * @return 返回内存中缓存的图像，第一次加载返回null
	 */
	public static Bitmap asyncLoadIconImage(final String imagePath,
			final ImageCallback callback) {

		MyLog.info("asyncLoadIcon", "imagePath = " + imagePath);

//		Bitmap image = ImageManager.getImageFromCache(imagePath);
//		if (image != null)
//			return image;
//		else {			
//			loadImageVector.add(new Object[] { imagePath, callback });
//			startThread();
//		}
		loadImageVector.add(new Object[] { imagePath, callback, "icon" });
		startThread();

		return null;
	}
	
	/**
	 * 
	 * @param imageUrl
	 *            图像url地址
	 * @param callback
	 *            回调接口
	 * @return 返回内存中缓存的图像，第一次加载返回null
	 */
	public static Bitmap asyncLoadDetailImage(final String imagePath,
			final ImageCallback callback) {

		MyLog.info("asyncLoadImage", "imagePath = " + imagePath);

//		Bitmap image = ImageManager.getImageFromCache(imagePath);
//		if (image != null)
//			return image;
//		else {			
//			loadImageVector.add(new Object[] { imagePath, callback });
//			startThread();
//		}
		loadImageVector.add(new Object[] { imagePath, callback, "detail"});
		startThread();

		return null;
	}

	// 对外界开放的回调接口
	public interface ImageCallback {
		// 注意 此方法是用来设置目标对象的图像资源
		public void imageLoaded(Bitmap image);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (threadRuning) {
			for (int i = 0; i < loadImageVector.size(); i++) {
				Object[] loadImageObject = loadImageVector.elementAt(i);
				loadImageVector.remove(i);
				Bitmap image = null;
//				if (((String) loadImageObject[2]).equals("icon")){
//					image = ImageManager
//							.getIconImage((String) loadImageObject[0]);
//				}
//				else if (((String) loadImageObject[2]).equals("detail")){
//					image = ImageManager
//							.getDetailImage((String) loadImageObject[0]);
//				}
				image = ImageManager
						.getImageFromNet((String) loadImageObject[0]);
				final Bitmap finalImage = image;
				if (finalImage != null) {
					final ImageCallback callback = (ImageCallback) loadImageObject[1];
					handler.post(new Runnable() {
						public void run() {
							callback.imageLoaded(finalImage);
						}
					});
				} else {
				}
			}
			try {
				Thread.sleep(0);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
